<template>
	<div class="padding-across-10 main" v-loading="loading" element-loading-text="拼命加载中">
		<div style="padding-top: 5px;padding-bottom: 2px">
			<headerOpreate style="width: 200px;float: left" :opreate-datas="opreateDatas" v-on:executeAction="executeAction"></headerOpreate>
			<div style="display: flex;float: right">
				<div style="display: flex;">
					<el-select size="small" v-model="rank" :placeholder="$t('message.selectModeAlert')" style="width: 80px;margin-right: 5px" @change="selectRank">
						<el-option label="降序" :value="0"></el-option>
						<el-option label="升序" :value="1"></el-option>
					</el-select>
				</div>
				<div style="float: right;padding-bottom: 3px">
					<span style="font-size: 10px;">{{$t('message.start')+':&nbsp;&nbsp;'}}</span>
					<el-date-picker :editable="false" :clearable="false" v-model="startTime" size="small" type="date" placeholder="选择起始日期" @change="changeStartTime"></el-date-picker>
					<span style="font-size: 10px">{{$t('message.end')+':&nbsp;&nbsp;'}}</span>
					<el-date-picker :editable="false" :clearable="false" size="small" type="date" v-model="endTime" placeholder="选择结束日期" @change="changeEndTime"></el-date-picker>
				</div>
			</div>
		</div>

		<el-table border style="width: 100%;" :data="tableData">
			<el-table-column type="index" align="center"></el-table-column>
			<el-table-column label="书名" align="center" prop="name"></el-table-column>
			<el-table-column label="作者" align="center" prop="author"></el-table-column>
			<el-table-column label="借阅次数" align="center" prop="count"></el-table-column>
			<el-table-column label="类型" align="center" prop="type"></el-table-column>
			<el-table-column label="状态" align="center">
				<template slot-scope="scope">
					<el-tag type="danger" v-if="scope.row.status === 1">丢失</el-tag>
					<el-tag type="warning" v-if="scope.row.status === 2">外借</el-tag>
					<el-tag type="success" v-if="scope.row.status === 3">在库</el-tag>
				</template>
			</el-table-column>
		</el-table>
		<div class="block" style="text-align:right;margin-bottom:100px;padding-top: 2px;padding-bottom: 5px">
			<el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :current-page="currentPage" :page-size="pageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="totalSize">
			</el-pagination>
		</div>
	</div>
</template>
<script>
	import $ from 'jquery'
	import { GloabFun,HttpServer } from '../../../assets/js/common.js'
	import headerOpreate from '../../../components/headerOpreate'
	let id = 1000;
	var date = new Date();
	export default {
		components: {
			headerOpreate
		},
		data() {
			Date.prototype.format = function(format) {
				var date = {
					"M+": this.getMonth() + 1,
					"d+": this.getDate()
				};
				if(/(y+)/i.test(format)) {
					format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
				}
				for(var k in date) {
					if(new RegExp("(" + k + ")").test(format)) {
						format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
							date[k] : ("00" + date[k]).substr(("" + date[k]).length));
					}
				}
				return format;
			}
			return {
				isReload: false,
				startTime: new Date((date.getTime() - 3600 * 1000 * 24 * 30)).format('yyyy-MM-dd') + ' 00:00:00',
				endTime: date.format('yyyy-MM-dd') + ' 23:59:59',
				loading: false,
				rank: 0,
				tableData: [],
				multipleSelection: [],
				currentPage: 1,
				pageSize: 10,
				totalSize: 0,
				opreateDatas: [{
					show: true,
					size: "small",
					type: 'primary',
					icon: 'el-icon-eleg-icon_refresh el-icon--left',
					name: '刷新',
					action: 'reloadDatas'
				}]
			}
		},
		props: {
			subArgs: {
				type: Object,
				default: function() {
					return {};
				}
			}
		},
		methods: {
			executeAction(action) {
				if(action) {
					this[action]();
				}
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDatas();
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getDatas();
			},
			//选择升序降序
			selectRank() {
				this.getDatas();
			},
			changeStartTime() {
				this.startTime = this.startTime.format('yyyy-MM-dd') + ' 00:00:00';
				this.getDatas();
			},
			changeEndTime() {
				this.endTime = this.endTime.format('yyyy-MM-dd') + ' 23:59:59';
				this.getDatas();
			},
			//刷新
			reloadDatas() {
				this.isReload = true;
				this.getDatas();
			},
			//查询统计数据
			getDatas() {
				this.loading = true;
				HttpServer.httpPost.call(this, '/Api/Book/Info/BorrowRanking', {
					begin: this.startTime,
					end: this.endTime,
					pagesize: this.pageSize,
					index: this.currentPage,
					order: this.rank
				}, function(response) {}, function(response) {
					// console.log('response',response)
					this.loading = false;
					if(response.data.code === 0) {
						this.tableData = response.data.info;
						this.totalSize = response.data.totalcount;
						if(this.isReload){
							this.$message({
								type: 'success',
								message: this.$t('message.refreshSuccess')
							});
						}
					} else {
						this.$message.error(response.data.msg);
					}
					this.isReload = false;
				})
			},
		},
		created() {
			if(GloabFun.getReaderCode() != ''){
				this.getDatas();
			}else{
				this.$alert('读者尚未登录', this.$t('message.warning'), {
					confirmButtonText: this.$t('message.yes'),
					type: 'warning'
				});
			} 
		}
	}
</script>
<style scoped lang="less">
	.main {
		background-color: #fff;
		border-radius: 10px;
	}
	
	.name {
		color: #20a0ff;
	}
	
	.el-date-editor.el-input {
		width: 130px;
	}
	
	.edit-mission {
		padding-top: 50px;
		.el-dialog__header {
			background-color: #000;
		}
		.el-dialog__body {
			position: relative;
		}
		.clearfloat {
			height: 100px;
			max-height: 150px;
		}
	}
</style>